package com.longrise.android.byjk.plugins.dealsituation.cachecourse;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.CurrcluSubTitle;
import com.longrise.android.byjk.model.CurriclunTitle;

import java.util.List;

/**
 * Created by xkj on 2016/3/11.
 * 添加缓存的adapter
 */
public class AddDownLoadAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<CurriclunTitle> mParentList;
    private List<List<CurrcluSubTitle>> mChildList;

    public AddDownLoadAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<CurriclunTitle> currList, List<List<CurrcluSubTitle>> cSubTitle) {
        this.mParentList = currList;
        this.mChildList = cSubTitle;
    }

    @Override
    public int getGroupCount() {
        if (mParentList != null) {
            return mParentList.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (mChildList != null) {
            return mChildList.get(groupPosition).size();
        } else {
            return 0;
        }

    }

    @Override
    public Object getGroup(int groupPosition) {
        if (mParentList != null) {
            return mParentList.get(groupPosition);
        } else {
            return null;
        }

    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (mChildList != null) {
            return mChildList.get(groupPosition).get(childPosition);
        } else {
            return null;
        }

    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentHolder parentHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_adddownload_title, null);
            parentHolder = new ParentHolder();
            parentHolder.mViewLine = convertView.findViewById(R.id.adddownload_top_line);
            parentHolder.mTv = (TextView) convertView.findViewById(R.id.adddownload_parent_tv);
            parentHolder.mIv = (ImageView) convertView.findViewById(R.id.adddownload_parent_iv);
            convertView.setTag(parentHolder);
        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }

        if (isExpanded) {
            parentHolder.mIv.setImageResource(R.drawable.icon_up);
        } else {
            parentHolder.mIv.setImageResource(R.drawable.icon_down);
        }

        if (groupPosition == 0) {
            parentHolder.mViewLine.setVisibility(View.GONE);
        } else {
            parentHolder.mViewLine.setVisibility(View.VISIBLE);
        }

        if (mParentList != null) {
            CurriclunTitle titlebean = mParentList.get(groupPosition);
            parentHolder.mTv.setText(titlebean.getTitle());
        }
        return convertView;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_adddownload_child, null);
            childHolder = new ChildHolder();
            childHolder.mTvtitle = (TextView) convertView.findViewById(R.id.adddownload_child_tv_title);
            childHolder.mIv = (ImageView) convertView.findViewById(R.id.adddownload_child_iv);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        if (mChildList != null) {
            CurrcluSubTitle childbean = mChildList.get(groupPosition).get(childPosition);
            String childTitle = childbean.getTitle();
            childHolder.mTvtitle.setText(childTitle);
            if (childbean.isDownloaded()) {
                childHolder.mIv.setImageResource(R.drawable.finish);
            } else  if (childbean.isCheck()) {
                childHolder.mIv.setImageResource(R.drawable.circle_check);
            } else {
                childHolder.mIv.setImageResource(R.drawable.circle);
            }{


            }

        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ParentHolder {
        private View mViewLine;

        private TextView mTv;

        private ImageView mIv;
    }

    class ChildHolder {
        private TextView mTvtitle;
        private ImageView mIv;
    }
}
